
import java.util.HashMap;
import java.util.Map;


public class parse {
        Map<String, String> vars;
        
        public parse()
        {
                vars = new HashMap<String, String>();
        }
        public String doEquation(String statement)
        {
                String[] chars = statement.split(" ");
                try{
                        String op = chars[1];
                        
                        if(vars.get(op) != null)
                        {
                                op = vars.get(op);
                        }
                        if(op.equals("="))
                        {
                                try{
                                        vars.put(chars[0], chars[2]);
                                        return "Variable " + chars[0] + " set to " + chars[2] + "!";
                                }
                                catch(Exception e)
                                {
                                        System.out.println("Please enter a double for a set var statement.");
                                }
                        }
                        else
                        {
                                double firstNum = 0;
                                double secondNum = 0;
                                if(vars.containsKey(chars[0]))
                                {
                                        firstNum = Double.parseDouble(vars.get(chars[0]));
                                }
                                else
                                {
                                        firstNum = Double.parseDouble(chars[0]);
                                }
                                
                                if(vars.containsKey(chars[2]))
                                {
                                        secondNum = Double.parseDouble(vars.get(chars[0]));
                                }
                                else
                                {
                                        secondNum = Double.parseDouble(chars[2]);       
                                }
                                
                                if(op.equals("+"))
                                {
                                        double result = firstNum + secondNum;
                                        String resultS = Double.toString(result) + "";
                                        return resultS;
                                }
                                if(op.equals("*"))
                                {
                                        double result = firstNum * secondNum;
                                        String resultS = Double.toString(result);
                                        return resultS;
                                }
                                if(op.equals("/"))
                                {
                                        double result = firstNum / secondNum;
                                        String resultS = Double.toString(result);
                                        return resultS;
                                }
                                if(op.equals("-"))
                                {
                                        double result = firstNum - secondNum;
                                        String resultS = Double.toString(result);
                                        return resultS;
                                }
                                else
                                {
                                        return "Operation not supported!";
                                }
                        }
                }
                catch(Exception e)
                {
                        System.out.println("Invalid statment.  Remember: Your statement must be in the format var1 op var2");

                }
                return "";
        }
}